package com.zxl.manager.privacy.utils.database;

import android.content.ContentProvider;
import android.content.ContentResolver;
import android.content.ContentValues;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.net.Uri;
import android.text.TextUtils;

import com.zxl.manager.privacy.utils.ApplicationHelper;
import com.zxl.manager.privacy.utils.utils.LogUtils;

/**
 * Created by Administrator on 2016/7/4.
 */
public abstract class BaseContentProvider extends ContentProvider {

    private String mAuthority;

    private Uri mDefaultSqlUri;

    private SQLiteOpenHelper mHelper;

    private ContentResolver mResolver;

    @Override
    public final boolean onCreate() {
        boolean isSuccess = false;
        try {
            mHelper = getHelper();
            mResolver = getResolver();
            mAuthority = getAuthority();
            if (null == mHelper || mResolver == null || mAuthority == null) {
                throw new RuntimeException("初始化失败");
            }
            mDefaultSqlUri = getDefaultSqlUri();
            isSuccess = true;
        } catch (Throwable e) {
            e.printStackTrace();
        }
        return isSuccess;
    }

    @Override
    public final String getType(Uri uri) {
        if (mDefaultSqlUri.equals(uri)) {
            return mDefaultSqlUri.toString();
        }
        return getTableName(uri);
    }

    @Override
    public final Cursor query(Uri uri, String[] projection, String selection, String[] selectionArgs, String sortOrder) {
        String type = getType(uri);
        if (!TextUtils.isEmpty(type)) {
            if (type.equals(mDefaultSqlUri.toString())) {
                return mHelper.getReadableDatabase().rawQuery(selection, selectionArgs);
            } else {
                return mHelper.getReadableDatabase().query(type, projection, selection, selectionArgs, null, null, sortOrder);
            }
        }
        return null;
    }

    @Override
    public final Uri insert(Uri uri, ContentValues values) {
        LogUtils.d("insert : " + uri + ", " +values);
        String type = getType(uri);
        if (!TextUtils.isEmpty(type)) {
            if (!type.equals(mDefaultSqlUri.toString())) {
                boolean isSuccess = false;
                try {
                    mHelper.getWritableDatabase().insert(type, null, values);
                    isSuccess = true;
                    return uri;
                } catch (Throwable e) {
                    e.printStackTrace();
                } finally {
                    if (isSuccess) {
                        mResolver.notifyChange(uri,null);
                    }
                }
            } else {
                throw new RuntimeException("no support insert by sql");
            }
        }
        return null;
    }

    @Override
    public final int delete(Uri uri, String selection, String[] selectionArgs) {
        LogUtils.d("delete : " + uri + ", + " +selection + ", " + selectionArgs);
        String type = getType(uri);
        boolean isSuccess = false;
        if (!TextUtils.isEmpty(type)) {
            try {
                if (type.equals(mDefaultSqlUri.toString())) {
                    isSuccess = execSQL(mHelper.getWritableDatabase(), selectionArgs);
                } else {
                    mHelper.getWritableDatabase().delete(type, selection, selectionArgs);
                    isSuccess = true;
                }
            } catch (Throwable e) {
                e.printStackTrace();
            } finally {
                if (isSuccess) {
                    mResolver.notifyChange(uri, null);
                }
            }
        }
        return isSuccess ? 1 : -1;
    }

    @Override
    public final int update(Uri uri, ContentValues values, String selection, String[] selectionArgs) {
        LogUtils.d("update : " + uri + ", + " + values + "," +selection + ", " + selectionArgs);
        String type = getType(uri);
        boolean isSuccess = false;
        if (!TextUtils.isEmpty(type)) {
            try {
                if (type.equals(mDefaultSqlUri.toString())) {
                    isSuccess = execSQL(mHelper.getWritableDatabase(), selectionArgs);
                } else {
                    mHelper.getWritableDatabase().update(type, values, selection, selectionArgs);
                    isSuccess = true;
                }
            } catch (Throwable e) {
                e.printStackTrace();
            } finally {
                if (isSuccess) {
                    mResolver.notifyChange(uri, null);
                }
            }
        }
        return isSuccess ? 1 : -1;
    }

    private boolean execSQL(SQLiteDatabase database, String... sql) {
        boolean result = false;
        if (sql == null || sql.length == 0) {
            return result;
        }
        try {
            database.beginTransaction();
            for (String string : sql) {
                database.execSQL(string);
            }
            database.setTransactionSuccessful();
            result = true;
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            database.endTransaction();
        }
        return result;
    }

    /**
     * 获取标识
     * @return
     */
    public abstract String getAuthority();

    /**
     * 通过uri获取表名
     * @param uri
     * @return
     */
    public abstract String getTableName(Uri uri);

    /**
     * 获取数据观察者
     * @return
     */
    public abstract ContentResolver getResolver();

    /**
     * 获取数据库对象
     * @return
     */
    public abstract SQLiteOpenHelper getHelper();

    /**
     * 获取默认SQL URI
     * @return
     */
    public abstract Uri getDefaultSqlUri();

    /**
     * 数据查询
     * @param uri
     * @param sql
     * @param selectionArgs
     * @return
     */
    public static Cursor query(Uri uri, String sql, String[] selectionArgs) {
        return ApplicationHelper.getContext().getContentResolver().query(uri, null, sql, selectionArgs, null);
    }

    /**
     * 删除
     * @param uri
     * @param sql
     */
    public static void delete(Uri uri, String... sql) {
        ApplicationHelper.getContext().getContentResolver().delete(uri, null, sql);
    }

    /**
     * 更新
     * @param uri
     * @param sql
     */
    public static void update(Uri uri, String... sql) {
        ApplicationHelper.getContext().getContentResolver().update(uri, new ContentValues(), null, sql);
    }



}
